package cn.usian.service.impl;

import cn.usian.common.R;
import cn.usian.dao.CategoryDao;
import cn.usian.dao.DishDao;
import cn.usian.dao.SetmealDao;
import cn.usian.exception.CustomerException;
import cn.usian.exception.NameException;
import cn.usian.pojo.Category;
import cn.usian.pojo.Dish;
import cn.usian.pojo.Setmeal;
import cn.usian.service.CategoryService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

@Service
@Transactional
public class CategoryServiceImpl implements CategoryService {
    @Resource
    private CategoryDao categoryDao;
    @Resource
    private SetmealDao setmealDao;
    @Resource
    private DishDao dishDao;



    //分页排序
    @Override
    public R findPage(int page, int pageSize) {
        //创建page对象
        Page<Category> categoryPage = new Page<>(page, pageSize);
       //增加排序的条件
        LambdaQueryWrapper<Category> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.orderByAsc(Category::getSort);
        Page<Category> selectPage = categoryDao.selectPage(categoryPage, queryWrapper);
        return R.success(selectPage);
    }

    //添加
    @Override
    public void save(Category category) {
        //判断分类名是否存在
        if (findByName(category.getName())!=null){
            throw  new NameException(category.getName());
        }
        category.setCreateTime(LocalDateTime.now());
        category.setUpdateTime(LocalDateTime.now());
        categoryDao.insert(category);
    }

    //查询名字
    @Override
    public Category findByName(String name) {
        LambdaQueryWrapper<Category> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Category::getName,name);
        return categoryDao.selectOne(queryWrapper);
    }
    //修改
    @Override
    public void update(Category category) {
            categoryDao.updateById(category);
    }

    //删除
    @Override
    public void del(Long id) {
        //根据id查询dish表和setmeal表中师傅在关联数据
        LambdaQueryWrapper<Dish> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Dish::getCategoryId,id);
        List<Dish> dishList = dishDao.selectList(queryWrapper);
        //判断dishList是否为空
        if (!dishList.isEmpty()){
            //触发自定义的关联菜品异常
            throw  new CustomerException("该分类已关联其他套餐不能删除");
        }
        LambdaQueryWrapper<Setmeal> queryWrapper1 = new LambdaQueryWrapper<>();
        queryWrapper1.eq(Setmeal::getCategoryId,id);
        List<Setmeal> setmealList = setmealDao.selectList(queryWrapper1);
        if (!setmealList.isEmpty()){
            throw new CustomerException("该分类已关联其他套餐不能删除");
        }
        categoryDao.deleteById(id);
    }

    @Override
    public List<Category> findAll() {
        return categoryDao.selectList(null);
    }

    @Override
    public R list(int type) {
        LambdaQueryWrapper<Category> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Category::getType,type);
        return R.success(categoryDao.selectList(queryWrapper));
    }

    @Override
    public void insert(Category category) {
        categoryDao.insert(category);
    }


}
